package org.wedding.center.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.wedding.center.service.ServiceTypeService;
import org.wedding.center.mapper.ServiceTypeMapper;
import org.wedding.center.entity.ServiceType;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wedding.enums.system.ResultCodeEnum;
import org.wedding.utils.CommonResult;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import org.wedding.dto.PageModel;
import org.wedding.utils.PageUtil;
import org.springframework.transaction.annotation.Transactional;
import org.wedding.center.dto.query.ServiceTypeQuery;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 服务类别表(wedding) 表服务实现类
 *
 * @author 2025-07-05 19:35:45
 * @since sky
 */
@Service
public class ServiceTypeServiceImpl extends ServiceImpl<ServiceTypeMapper, ServiceType> implements ServiceTypeService {

    /**
     * 分页查询所有数据
     *
     * @return
     */
    @Override
    public CommonResult<ServiceTypeQuery> findAll(ServiceTypeQuery query) {
        try {
            if (StrUtil.isNotEmpty(query.getCondition()) && "1".equals(query.getCondition())) {
                //查询价格条件区间数据
                query.setCondition(null);
            }
            List<ServiceType> list = baseMapper.selectAll(query);
            List<ServiceType> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), list), list.size());
            query.setList(setList, list.size());
            return CommonResult.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }


    /**
     * 根据id查看详情
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<ServiceType> view(String id) {
        try {
            ServiceType serviceType = baseMapper.findById(id);
            return CommonResult.success(serviceType);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param serviceType
     * @return
     */
    @Override
    public CommonResult add(ServiceType serviceType) {
        try {
            String userId = StpUtil.getSession().get("userId").toString();
            String userName = StpUtil.getSession().get("userName").toString();
            serviceType.setUserId(userId);
            serviceType.setUserName(userName);
            serviceType.setId(IdUtil.simpleUUID());
            serviceType.setCreateTime(DateUtil.now());
            serviceType.setDelFlag("0");
            baseMapper.insert(serviceType);
            return CommonResult.success(serviceType);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 修改数据
     *
     * @param serviceType
     * @return
     */
    @Override
    public CommonResult updateEntity(ServiceType serviceType) {
        try {
            serviceType.setUpdateTime(DateUtil.now());
            baseMapper.updateById(serviceType);
            return CommonResult.success(serviceType);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult delete(List<String> ids) {
        try {
            if (ObjectUtil.isEmpty(ids)) return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            for (String id : ids) {
                baseMapper.upDeleteById(id);
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

}